/*
 * Licensed under the MIT License
 * 
 * Copyright (c) 2011 Kay Kreuning
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * http://code.google.com/p/hyves-as3-sdk/
 */
package nl.hyves.api.net
{
	import com.adobe.serialization.json.JSONDecoder;
	import nl.hyves.api.utils.JSONMapper;
	import nl.hyves.api.net.IHyvesResponse;
	import nl.hyves.api.errors.HyvesError;
	
	public class HyvesResponse implements IHyvesResponse
	{
		protected static const ERROR_PATTERN:RegExp = /^\{"method":"error",/;
		protected static const CURRENT_PAGE_PATTERN:RegExp = /(?<="currentpage":)\d+/;
		protected static const TOTAL_PAGES_PATTERN:RegExp = /(?<="totalpages":)\d+/;
		protected static const TOTAL_RESULTS_PATTERN:RegExp = /(?<="totalresults":)\d+/;
		protected static const RESULTS_PER_PAGE_PATTERN:RegExp = /(?<="resultsperpage":)\d+/;
		
		protected var _raw:String;
		protected var _data:Object;
		protected var _error:HyvesError;
		protected var _currentPage:int;
		protected var _totalPages:int;
		protected var _totalResults:int;
		protected var _resultsPerPage:int;
		
		public function get success():Boolean
		{
			return !ERROR_PATTERN.test(raw);
		}

		public function get raw():String
		{
			return _raw;
		}

		public function get data():*
		{
			if (raw && success)
			{
				if (!_data)
				{
					_data = JSONMapper.map(raw);
				}
				
				return _data;
			}
			else
			{
				return null;
			}
		}

		public function get error():HyvesError
		{
			if (raw && !success)
			{
				if (!_error)
				{
					var json:Object = new JSONDecoder(raw, true).getValue();
					var method:String;
					var parameters:Array = json.request_parameters.parameter;
					
					for each (var parameter:Object in parameters)
					{
						if (parameter.requestkey == 'ha_method')
						{
							method = parameter.requestvalue;
							break;
						}
					}
					
					_error = new HyvesError(json.error_message.replace('\n', ''), parseInt(json.error_code), method);
				}
				
				return _error;
			}
			else
			{
				return null;
			}
		}
		
		public function get currentPage():int
		{
			if (!_resultsPerPage && RESULTS_PER_PAGE_PATTERN.test(raw))
			{
				_resultsPerPage = RESULTS_PER_PAGE_PATTERN.exec(raw)[0];
			}
			
			return _resultsPerPage;
		}
		
		public function get totalPages():int
		{
			if (!_totalPages && TOTAL_PAGES_PATTERN.test(raw))
			{
				_totalPages = TOTAL_PAGES_PATTERN.exec(raw)[0];
			}
			
			return _totalPages;
		}
		
		public function get totalResults():int
		{
			if (_totalResults && TOTAL_RESULTS_PATTERN.test(raw))
			{
				_totalResults = TOTAL_RESULTS_PATTERN.exec(raw)[0];
			}
			
			return _totalResults;
		}
		
		public function get resultsPerPage():int
		{
			if (_resultsPerPage && RESULTS_PER_PAGE_PATTERN.test(raw))
			{
				_resultsPerPage = RESULTS_PER_PAGE_PATTERN.exec(raw)[0];
			}
			
			return _resultsPerPage;
		}
		
		public function HyvesResponse(raw:String)
		{
			_raw = raw;
		}
	}
}
